package com.kingdee.pda.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kingdee.pda.R;
import com.kingdee.pda.entity.ReplenishSkuBean;
import com.kingdee.pda.ui.dialog.ReplenishUpdateNumDialog;
import com.kingdee.pda.ui.widget.CustomHScrollView;
import com.kingdee.pda.util.ViewHolder;

/**
 * 补货单SKU列表适配器
 * Created by dlj
 * DATE 2021/6/26
 * TIME 16:24
 */
public class InnerReplenishSkuListAdapter extends AbsBaseAdapter<ReplenishSkuBean> {

    private final OnSkuItemClickListener onSkuItemClickListener;
    private CustomHScrollView customHScrollView;

    public InnerReplenishSkuListAdapter(Context context, OnSkuItemClickListener onSkuItemClickListener, CustomHScrollView customHScrollView) {
        super(context);
        this.onSkuItemClickListener = onSkuItemClickListener;
        this.customHScrollView = customHScrollView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_item_four_textview, null);
        }
        TextView tv1 = ViewHolder.get(convertView, R.id.tv1);
        TextView tv_invStatusName = ViewHolder.get(convertView, R.id.tv_invStatusName);
        TextView tv2 = ViewHolder.get(convertView, R.id.tv2);
        TextView tv3 = ViewHolder.get(convertView, R.id.tv3);
        TextView tv4 = ViewHolder.get(convertView, R.id.tv4);
        LinearLayout item = ViewHolder.get(convertView, R.id.item);
        //  头部和item的滑动事件绑定
        CustomHScrollView contentScorllView = convertView.findViewById(R.id.h_scrollView);
//        customHScrollView.AddOnScrollChangedListener(new MyOnScrollChangedListener(contentScorllView));

        try {
            ReplenishSkuBean bean = getData().get(position);
            tv1.setText(bean.getItemBarCode());
            tv_invStatusName.setText(bean.getInvStatusName());
            tv2.setText(bean.getFromLocationNumber() + "：" + bean.getWantQty());
            String actulString = "";
            for (int i = 0; i < bean.getActulBeans().size(); i++) {
                ReplenishSkuBean.ActulBean actulBean = bean.getActulBeans().get(i);
                if (i < bean.getActulBeans().size() - 1) {
                    actulString = actulString + actulBean.getActualFromLocationNumber() + ">" + actulBean.getTargetLocationNumber() + "：" + actulBean.getActulQty() + "\n";
                } else {
                    actulString = actulString + actulBean.getActualFromLocationNumber() + ">" + actulBean.getTargetLocationNumber() + "：" + actulBean.getActulQty();
                }
            }

            tv3.setText(actulString);
            tv3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //弹出修改数量框
                    ReplenishUpdateNumDialog.showUpdateNumDialog(bean.getActulBeans(), context, new ReplenishUpdateNumDialog.InputListener() {
                        @Override
                        public void okEvent(DialogInterface dialog) {
                            dialog.dismiss();
                            notifyDataSetChanged();
                        }
                    });
                }
            });
            tv4.setText((bean.getRealQty() - bean.getWantQty()) + "");
            item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onSkuItemClickListener.onSkuItemClick(position);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    public interface OnSkuItemClickListener {
        void onSkuItemClick(int position);
    }
}
